﻿using Backend.Data.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Backend.Data.Managers
{
    public class LegalEntityManager
    {



        public static IList<LegalEntity> GetListByFilter(string filter)
        {
            //throw new NotImplementedException();
            using (BackendDbContext ctx = new BackendDbContext())
            {
                return ctx.LegalEntitys.Where(t => t.Code.Contains(filter)).ToList();
            }
        }


        public static LegalEntity Create(LegalEntity legalEntity)
        {
            using (BackendDbContext ctx = new BackendDbContext())
            {
                LegalEntity d = ctx.LegalEntitys.Add(legalEntity);
                ctx.SaveChanges();
                return d;
            }
        }


        public static LegalEntity Update(LegalEntity legalEntity)
        {
            using (BackendDbContext ctx = new BackendDbContext())
            {
                ctx.Entry(legalEntity).State = EntityState.Modified;
                ctx.SaveChanges();
                return legalEntity;
            }
        }


        public static LegalEntity GetById(Guid id)
        {
            using (BackendDbContext ctx = new BackendDbContext())
            {
                LegalEntity legalEntity = ctx.LegalEntitys.Find(id);
                if (legalEntity == null)
                {
                    throw new Exception("Legal Entity no found in data base.");
                }
                return legalEntity;
            }
        }
        public static void Delete(Guid id)
        {
            using (BackendDbContext ctx = new BackendDbContext())
            {
                LegalEntity legalEntity = ctx.LegalEntitys.Find(id);
                if (legalEntity == null)
                {
                    throw new Exception("Legal Entity Ent no found in data base.");
                }
                ctx.LegalEntitys.Remove(legalEntity);
                ctx.SaveChanges();
            }
        }


    }
}
